<?php
namespace drivers;

abstract class Controller extends \Smarty
{
    public $child;
    function __construct(){
        parent::__construct();
        $arr = explode('\\', get_called_class());
        $this->child = 'templates/'.mb_strtolower(str_replace('Controller','',end($arr)));

        $smartConf = factory::config()->getConfig('smarty');
        $this->setTemplateDir($smartConf['TemplateDir']);
        $this->setCompileDir($smartConf['CompileDir']);
        $this->setConfigDir($smartConf['ConfigDir']);
        $this->setCacheDir($smartConf['CacheDir']);
        $this->debugging = $smartConf['debugging'];
        $this->left_delimiter = $smartConf['left_delimiter'];
        $this->right_delimiter = $smartConf['right_delimiter'];
        $this->caching = \Smarty::CACHING_LIFETIME_CURRENT;
        $this->assign('app_name','LzFramework');
    }

    function render($views,$args = []){
        $file = __VROOT__.$this->child.'/'.$views;
        $ext = file_exists($file.'.html') ? '.html' : (file_exists($file.'.php') ? '.php' : (file_exists($file.'.tpl') ? '.tpl' : (file_exists($file) ? '' : false)));
        if ($ext){
            extract($args);
            include($file.$ext);
        }else{
            throw new TException("模板文件".$views.'不存在');
        }
    }
}